﻿using Microsoft.EntityFrameworkCore;
using MyMes.Infrastructure;
using MyMES.IServices.PlanInfos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Utilities.Response;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.Core;

namespace MyMES.Services.PlanInfos
{
    public class PlanInfoServices:IPlanInfoServices
    {
        private readonly IBaseRepository<PlanInfo> _planInfoRepository;

        public PlanInfoServices(IBaseRepository<PlanInfo> planInfoRepository)
        {
            _planInfoRepository = planInfoRepository;
        }
        /// <summary>
        /// 添加质检方案信息
        /// </summary>
        /// <param name="planInfo">质检方案实体</param>
        /// <returns></returns>
        public async Task<WebResponseContent> AddPlanInfoModel(PlanInfo planInfo)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {
                await _planInfoRepository.AddAsync(planInfo);
                return response.OK("添加质检方案成功");
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除质检方案信息
        /// </summary>
        /// <param name="planInfo">质检方案实体</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> DelPlanInfoModel(PlanInfo planInfo)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {

                await _planInfoRepository.Remove(planInfo);
                return response.OK("删除质检方案成功");
            }
            catch (Exception ex)
            {
                return response.Error($"删除质检方案失败：{ex.Message}");
            }
        }

        public async Task<PageGridData<PlanInfo>> GetPlanInfo(string? PlanNo, string? PlanName, int pageIndex = 1, int pageSize = 10)
        {
            var pageData = new PageGridData<PlanInfo>();
            var query = _planInfoRepository.GetAll();

            if (!string.IsNullOrEmpty(PlanNo))
                query = query.Where(x => x.PlanNo == PlanNo);
            if (!string.IsNullOrEmpty(PlanName))
                query = query.Where(x => x.PlanName.Contains(PlanName));

            pageData.total = query.Count();



            pageData.rows = await query.OrderByDescending(x => x.Id)
                             .Skip((pageIndex - 1) * pageSize).Take(pageSize)
                                  .ToListAsync();
            return pageData;
        }

        /// <summary>
        /// 修改质检方案信息
        /// </summary>
        /// <param name="planInfo">质检方案实体</param>
        /// <returns></returns>
        public async Task<WebResponseContent> UpdatePlanInfoModel(PlanInfo planInfo)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {
                await _planInfoRepository.UpdateAsync(planInfo);
                return response.OK("更新质检方案成功");
            }
            catch (Exception ex)
            {
                return response.Error($"更新质检方案失败：{ex.Message}");
            }
        }
    }
}
